const path = require('path')
const webpack = require("webpack")
const merge = require('webpack-merge')
const baseWebpackconfig = require('./webpack.base.conf')
//优化重复的css
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
// css 压缩分离
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
//清除build之后的无用js
const CleanWebpackPlugin = require('clean-webpack-plugin');
//gzip 压缩
const CompressionWebpackPlugin = require('compression-webpack-plugin');
module.exports = merge.smart(baseWebpackconfig,{
    output: {
        filename: '[name]-[hash].js',
        libraryTarget: 'umd',
        path: path.resolve(__dirname, 'dist'),
        publicPath: './'
    },
    devtool: false,
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    {
                        loader: "sass-loader",
                        options: {
                            implementation: require("sass"), //使用dart-sass代替node-sass
                        }
                    },
                ]
            },
            {
                test: /\.less$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.css/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                ]
            },
        ]
    },
    optimization: {
        splitChunks: {
            name: false,
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'vendors',
                    chunks: chunk => chunk.name !== 'lib'
                },
            }
        },
        minimizer: [new OptimizeCssAssetsPlugin()],
    },
    stats: 'errors-only',
    plugins: [
        new CleanWebpackPlugin(),
        new webpack.HashedModuleIdsPlugin(),
        new MiniCssExtractPlugin({
            filename: "[name].css",
        }),
        new CompressionWebpackPlugin({
            threshold: 10240,
            test: /\.js$/,
        }),
    ]
})